#include "stdafx.h"
#include "RecordList.h"

using namespace System;
using namespace System::IO;
using namespace System::Windows::Forms;

RecordList::RecordList(){
	//Do nothing.
}

RecordList::RecordList(char *fileName){
	//header_nodes = new Node[100];
	dp.setFileName(fileName);
	header_info = "EMPTY";
	header_info = dp.readHeader();
	interpretHeader();
	int rsize = 0;
	int hsize = 4;
	for(int i=0; i<100; i++)
	{
		if(header_nodes[i].used)
		{
			switch(header_nodes[i].getDataType())
			{
			case 'A':
				rsize += sizeof(int) + sizeof(char);
				hsize += 30 + sizeof(char);
				break;
			case 'B':
				rsize += sizeof(double) + sizeof(char);
				hsize += 30 + sizeof(char);
				break;
			case 'C':
				rsize += sizeof(char) + sizeof(char);
				hsize += 30 + sizeof(char);
				break;
			case 'D':
				rsize += 30 + sizeof(char);
				hsize += 30 + sizeof(char);
				break;
			default:
				//
				break;
			}
		}
	}

	dp.readdp(header_count, hsize, rsize, header_nodes);
}

ArbRecord RecordList::searchForRecord(int recordNumber){
	// The correct diskpage is diskpage[recordNumber/7] and use its find function
	// to locate the record being searched for.
	// Return the record if found, if not found, return a null record
	// No longer needs the % 7
	/*Record R = dp.find(recordNumber);
	return R;*/

	int rsize = 0;
	int hsize = 4;
	for(int i=0; i<100; i++)
	{
		if(header_nodes[i].used)
		{
			switch(header_nodes[i].getDataType())
			{
			case 'A':
				rsize += sizeof(int) + sizeof(char);
				hsize += 30 + sizeof(char);
				break;
			case 'B':
				rsize += sizeof(double) + sizeof(char);
				hsize += 30 + sizeof(char);
				break;
			case 'C':
				rsize += sizeof(char) + sizeof(char);
				hsize += 30 + sizeof(char);
				break;
			case 'D':
				rsize += 30 + sizeof(char);
				hsize += 30 + sizeof(char);
				break;
			default:
				//
				break;
			}
		}
	}

	ArbRecord AR = dp.find(recordNumber, header_count, hsize, rsize, header_nodes);
	return AR;
}

bool RecordList::writeRecordsToDiskPage(int recordNumber){
	// Given a record, write the new record to the next consecutive
	// place available in the diskPage, if the diskpage is full,
	// write the remaining records to a new diskpage.
	dp.writedp();
	return true;
}

//bool RecordList::readInRecordAndStoreInArray(Record R, int pos){
//	dp.readdp();
//	recordList[pos] = R;
//	return true;
//}

bool RecordList::readInAsciiFile(char *fileName, int record){
	ifstream inAscii(fileName, ios::in);
	int recordNumber = record;

	long int id;					//Marked for deletion
	char* firstname;				//Marked for deletion
	char* lastname;					//Marked for deletion
	int streetnumber;				//Marked for deletion
	char* streetname;				//Marked for deletion
	char* city;						//Marked for deletion
	float balance;					//Marked for deletion
	
	char temp_char;
	int temp_index = 0;
	Record tempR;
	ArbRecord tempAR;

	header_count = 1;

	if(!inAscii)
	{
		return false;
	}
	while(!inAscii.eof() && inAscii.peek() >= 32)
	{
		char temp_id[30] = "                             ";			//Marked for deletion
		char temp_fn[30] = "                             ";			//Marked for deletion
		char temp_ln[30] = "                             ";			//Marked for deletion
		char temp_snum[30] = "                             ";		//Marked for deletion
		char temp_sname[30] = "                             ";		//Marked for deletion
		char temp_c[30] = "                             ";			//Marked for deletion
		char temp_b[30] = "                             ";			//Marked for deletion

		char temp_header_count[5] = "    ";
		
		//Read in the header

		while(inAscii.peek() != 32)
		{
			inAscii.get(temp_char);
			temp_header_count[temp_index++] = temp_char;
		}
		header_count = atoi(temp_header_count);
		inAscii.get(temp_char);
		temp_index = 0;

		char temp_header_info[5000];

		while(inAscii.peek() >= 32)
		{
			inAscii.get(temp_char);
			temp_header_info[temp_index++] = temp_char;
		}
		inAscii.get(temp_char);
		header_info = temp_header_info;
		temp_index = 0;

		int read = 0;

		for(int i=0;i<header_count;i++)
		{
			char temp_description[30] = "                             ";
			char temp_c;
			
			while(header_info[read] != 32)
			{
				temp_char = header_info[read];
				temp_description[temp_index++] = temp_char;
				read++;
			}
			temp_c = header_info[++read];

			header_nodes[i].setDataType(temp_c);
			header_nodes[i].setDescription(temp_description);
			read++; read++; temp_index = 0;
		}

		dp.writeHeader(header_nodes);

		while(!inAscii.eof() && inAscii.peek() >= 32)
		{
			for(int j=0; j<header_count; j++)
			{
				char temp_value[30] = "                             ";
				while(inAscii.peek() > 32)
				{
					inAscii.get(temp_char);
					temp_value[temp_index++] = temp_char;
				}
				switch(header_nodes[j].getDataType())
				{
					case 'A':
						tempAR.setNode(j, header_nodes[j].getDescription(), header_nodes[j].getDataType(), atoi(temp_value));
						break;
					case 'B':
						tempAR.setNode(j, header_nodes[j].getDescription(), header_nodes[j].getDataType(), atof(temp_value));
						break;
					case 'C':
						tempAR.setNode(j, header_nodes[j].getDescription(), header_nodes[j].getDataType(), (temp_value[0]));
						break;
					case 'D':
						tempAR.setNode(j, header_nodes[j].getDescription(), header_nodes[j].getDataType(), (temp_value));
						break;
					default:
						//
						break;
				}
				temp_index = 0;
				inAscii.get(temp_char);
			}

			dp.setAlist(recordNumber % 7, tempAR);
			recordNumber++;

			if(dp.isFull())
			{
				dp.writedp();
				dp.empty();
			}
		}

		dp.writedp();

		//
		// The following code is depriciated. While it is no longer needed, I hate to get rid of it just
		// in case it might prove useful later.
		//

		//while(inAscii.peek() != 32)
		//{
		//	inAscii.get(temp_char);
		//	temp_id[temp_index++] = temp_char;
		//}
		//id = atol(temp_id);
		//inAscii.get(temp_char);
		//temp_index = 0;

		//while(inAscii.peek() != 32)
		//{
		//	inAscii.get(temp_char);
		//	temp_fn[temp_index++] = temp_char;
		//}
		//firstname = temp_fn;
		//inAscii.get(temp_char);
		//temp_index = 0;

		//while(inAscii.peek() != 32)
		//{
		//	inAscii.get(temp_char);
		//	temp_ln[temp_index++] = temp_char;
		//}
		//lastname = temp_ln;
		//inAscii.get(temp_char);
		//temp_index = 0;

		//while(inAscii.peek() != 32)
		//{
		//	inAscii.get(temp_char);
		//	temp_snum[temp_index++] = temp_char;
		//}
		//streetnumber = atoi(temp_snum);
		//inAscii.get(temp_char);
		//temp_index = 0;

		//while(inAscii.peek() != 32)
		//{
		//	inAscii.get(temp_char);
		//	temp_sname[temp_index++] = temp_char;
		//}
		//streetname = temp_sname;
		//inAscii.get(temp_char);
		//temp_index = 0;

		//while(inAscii.peek() != ' ')
		//{
		//	inAscii.get(temp_char);
		//	temp_c[temp_index++] = temp_char;
		//}
		//city = temp_c;
		//inAscii.get(temp_char);
		//temp_index = 0;


		//char* bal_string;

		//while(inAscii.peek() > 32)
		//{
		//	inAscii.get(temp_char);
		//	temp_b[temp_index++] = temp_char;
		//	bal_string = temp_b;
		//}
		//bal_string = temp_b;
		//balance = (float)atof(bal_string);
		//inAscii.get(temp_char);
		//temp_index = 0;

		//tempR.setID(id);
		//tempR.setFirstName(firstname);
		//tempR.setLastName(lastname);
		//tempR.setStreetNumber(streetnumber);
		//tempR.setStreetName(streetname);
		//tempR.setCity(city);
		//tempR.setBalance(balance);

		//dp.setRlist(recordNumber % 7, tempR);
		//recordNumber++;

		////adding code here.
		//if(dp.isFull())
		//{
		//	dp.writedp();
		//	dp.empty();
		//}

	}

	/*dp.writedp();
	dp.empty();
	dp.readdp();
	return true;*/
}

void RecordList::setFileName(char *fileName){
	dp.setFileName(fileName);
}

int RecordList::getHeaderCount()
{
	return header_count;
}

Node RecordList::getHeaderNode(int node)
{
	return header_nodes[node];
}

void RecordList::interpretHeader()
{
	char * compare = "EMPTY";
	if((*header_info).GetHashCode() != (*compare).GetHashCode())
	{
		int position = 0;
		int temp_index = 0;
		char temp_char = 0;
		char temp_header_count[30] = "                             ";
		while(header_info[position] != 32)
		{
			temp_char = header_info[position++];
			temp_header_count[temp_index++] = temp_char;
		}
		header_count = atoi(temp_header_count);
		position++;
		temp_index = 0;

		char temp_header_info[5000];

		for(int j=0;j<5000;j++)
		{
			temp_header_info[j] = 32;
		}

		while(header_info[position] >= 32)
		{
			temp_char = header_info[position++];
			temp_header_info[temp_index++] = temp_char;
		}

		int temp_ftw = header_count;
		int temp_ftw_counter = 0;
		while(temp_ftw / 10 != 0)
		{
			temp_ftw /= 10;
			temp_ftw_counter++;
		}

		int read = temp_ftw_counter + 2;
		temp_index =0;

		for(int i=0;i<header_count;i++)
		{
			char temp_description[30] = "                             ";
			char temp_c;
			
			while(header_info[read] != 32)
			{
				temp_char = header_info[read];
				temp_description[temp_index++] = temp_char;
				read++;
			}
			temp_c = header_info[++read];

			header_nodes[i].setDataType(temp_c);
			header_nodes[i].setDescription(temp_description);
			read++; read++; temp_index = 0;
		}
	}
}